package com.meilai.project.service.media.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.meilai.project.dto.business.installOrder.MediaManualLockDTO;
import com.meilai.project.dto.business.intentionScheme.MediaPointSimpleVO;
import com.meilai.project.dto.business.intentionScheme.PointProcessDateRangeDTO;
import com.meilai.project.entity.media.MediaPointManualLock;
import com.meilai.project.entity.media.MediaPointOccupySnapshot;
import com.meilai.project.exception.CommonException;
import com.meilai.project.mapper.business.installOrder.MediaPointManualLockMapper;
import com.meilai.project.mapper.media.MediaPointOccupySnapshotMapper;
import com.meilai.project.service.business.intentionScheme.PointProcessService;
import com.meilai.project.service.media.MediaPointOccupySnapshotService;
import com.meilai.project.util.DateRangeUtil;
import com.meilai.project.util.ThreadLocalUserUtil;
import com.meilai.project.vo.business.installOrder.MediaPointLockDateRangeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 点位占用快照 服务实现类
 * </p>
 *
 * @author 张驰
 * @since 2022-01-25
 */
@Service
public class MediaPointOccupySnapshotServiceImpl extends ServiceImpl<MediaPointOccupySnapshotMapper, MediaPointOccupySnapshot> implements MediaPointOccupySnapshotService {

    @Autowired
    private MediaPointOccupySnapshotMapper mediaPointOccupySnapshotMapper;

    @Autowired
    private PointProcessService processService;

    @Autowired
    private MediaPointManualLockMapper mediaPointManualLockMapper;

    @Override
    public List<MediaPointLockDateRangeVO> listOccupyByMediaId(Long media_point_id) {
        List<MediaPointLockDateRangeVO> result = mediaPointOccupySnapshotMapper.listOccupyByMediaId(media_point_id);
        if (CollUtil.isNotEmpty(result)) {
            for (MediaPointLockDateRangeVO mediaPointLockDateRangeVO : result) {
                Integer type = mediaPointLockDateRangeVO.getType();
                String typeName = "";
                //1:锁位  2:合同  3:销售单
                if (1 == type) {
                    typeName = "锁位";
                } else if (2 == type) {
                    typeName = "合同";
                } else if (3 == type) {
                    typeName = "销售单";
                } else if (4 == type) {
                    typeName = "手动占用";
                }
                mediaPointLockDateRangeVO.setType_name(typeName);
            }
        }

        return result;
    }

    @Override
    public void submitManualLock(MediaManualLockDTO req) {
        List<MediaPointLockDateRangeVO> result = mediaPointOccupySnapshotMapper.listOccupyByMediaId(req.getMedia_point_id());
        if (result.size() > 0) {
            for (MediaPointLockDateRangeVO item : result) {
                boolean overlap = DateRangeUtil.isOverlap(req.getStart_at(), req.getEnd_at(), item.getStart_at(), item.getEnd_at());
                if (overlap) {
                    throw new CommonException("与现有区间" + item.getStart_at().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")) + "~" +
                            item.getEnd_at().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")) + "重合");
                }
            }
        }
        MediaPointManualLock mediaPointManualLock = new MediaPointManualLock();
        mediaPointManualLock.setUser_id(ThreadLocalUserUtil.get().getId());
        List<PointProcessDateRangeDTO> dateRangeDTOS = getPointProcessDateRangeDTOS(req);
        Long chooseId = processService.saveChoose(4, null, dateRangeDTOS);

        mediaPointManualLock.setChoose_id(chooseId);
        mediaPointManualLockMapper.insert(mediaPointManualLock);

        processService.lock(chooseId);
    }

    @Override
    public void releaseManualLockManualLock(Long chooseId) {
        MediaPointManualLock mediaPointManualLock = mediaPointManualLockMapper.getByChooseId(chooseId);
        if (null == mediaPointManualLock) {
            throw new CommonException("操作失败，所释放的锁定不存在！");
        }
        processService.releaseLock(chooseId);
    }

    private List<PointProcessDateRangeDTO> getPointProcessDateRangeDTOS(MediaManualLockDTO req) {
        List<PointProcessDateRangeDTO> dateRangeDTOS = new ArrayList<>();
        PointProcessDateRangeDTO item = new PointProcessDateRangeDTO();
        item.setBeginDate(req.getStart_at());
        item.setEndDate(req.getEnd_at());

        List<MediaPointSimpleVO> pointSimpleVOS = new ArrayList<>();
        MediaPointSimpleVO mediaPointSimpleVO = new MediaPointSimpleVO();
        mediaPointSimpleVO.setId(req.getMedia_point_id());
        pointSimpleVOS.add(mediaPointSimpleVO);

        item.setPointList(pointSimpleVOS);
        dateRangeDTOS.add(item);
        return dateRangeDTOS;
    }

    @Override
    public List<Long> getLockPointIdsByIds(List<Long> ids) {
        return baseMapper.getLockPointIdsByIds(ids);
    }
}
